const initState = {
  uid: 0,
  token: ''
}

/* const action = { type: '', data: { id: 1, name: 'aaa' } }

let {
  type,
  data: { id, name }
} = action */

const reducer = (state = initState, { type, data }) => {
  // 登录成功后记录
  if ('setUser' === type) {
    if (data) {
      // data数据源是存在，则为登录
      var { uid, token } = data
      // 把数据写入到本地
      sessionStorage.setItem('uid', uid)
      sessionStorage.setItem('token', token)
    } else {
      // 持化化的写入redux中
      var uid = +sessionStorage.getItem('uid')
      var token = sessionStorage.getItem('token')
    }
    return { ...state, uid, token }
  }

  if ('logout' === type) {
    sessionStorage.removeItem('uid')
    sessionStorage.removeItem('token')
    return { ...state, uid: 0, token: '' }
  }

  return state
}

export default reducer
